import pandas as pd
import matplotlib.pyplot as plt

# 读取所有文件并提取数据
files = {
    "0.5%": "python\\sh000300\\定投收益结果_触发条件_0.5%.xlsx",
    "0.8%": "python\\sh000300\\定投收益结果_触发条件_0.8%.xlsx",
    "1.0%": "python\\sh000300\\定投收益结果_触发条件_1.0%.xlsx",
    "1.5%": "python\\sh000300\\定投收益结果_触发条件_1.5%.xlsx",
    "2.0%": "python\\sh000300\\定投收益结果_触发条件_2.0%.xlsx",
    "3.0%": "python\\sh000300\\定投收益结果_触发条件_3.0%.xlsx",
    "无": "python\\sh000300\\定投收益结果.xlsx"
}

# 存储每个策略的总投入和总收益
results = {
    "策略": [],
    "总投入": [],
    "总收益": []
}

# 读取每个文件并提取数据
for threshold, file_name in files.items():
    df = pd.read_excel(file_name)
    
    # 提取最后一天的数据
    last_row = df.iloc[-1]
    total_investment = last_row["累计投入(元)"]
    total_profit = last_row["累计收益(元)"]
    
    results["策略"].append(threshold)
    results["总投入"].append(total_investment)
    results["总收益"].append(total_profit)

# 创建DataFrame
result_df = pd.DataFrame(results)

# 计算总收益率（总收益率 = 总收益 / 总投入）
result_df['总收益率'] = result_df['总收益'] / result_df['总投入']

# 绘制总收益率柱状图
plt.figure(figsize=(10, 6))
plt.bar(result_df["策略"], result_df["总收益率"], alpha=0.7)
plt.xlabel("策略")
plt.ylabel("总收益率")
plt.title("不同策略下的总收益率比较")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

# 绘制柱状图
plt.figure(figsize=(10, 6))
plt.bar(result_df["策略"], result_df["总投入"], alpha=0.7, label="总投入")
plt.bar(result_df["策略"], result_df["总收益"], bottom=result_df["总投入"], alpha=0.7, label="总收益")
plt.xlabel("策略")
plt.ylabel("金额（元）")
plt.title("不同策略下的总投入和总收益")
plt.xticks(rotation=45)
plt.legend()
plt.tight_layout()
plt.show()